package com.umeox.ota.engine;

import android.os.RecoverySystem;

import com.umeox.ota.data.local.CustomConfig;
import com.umeox.ota.utils.Utils;
import com.abupdate.iot_libs.OtaAgentPolicy;
import com.abupdate.iot_libs.data.constant.Error;
import com.abupdate.iot_libs.interact.callback.InstallListener;
import com.abupdate.iot_libs.interact.callback.UpdateInter;
import com.abupdate.trace.Trace;

import java.io.File;

/**
 * @author fighter_lee
 * @date 2018/9/25
 */
public class DebugUpdateEngine implements UpdateInter {
    public static final String TAG = DebugUpdateEngine.class.getSimpleName();

    @Override
    public void writePackage(String file, Callback callback) {
        try {
            if (Utils.isSupportAbupdate()) {
                OtaAgentPolicy.update().install(OtaAgentPolicy.getOtaEntityController().getMainEntity().getProductInfo().productId, new InstallListener() {
                    @Override
                    public void onUpgradeProgress(int i, int i1) {
                        callback.onUpgradeProgress(i, i1);
                    }

                    @Override
                    public void onUpgradeSuccess() {
                        callback.onUpgradeSuccess();
                    }

                    @Override
                    public void onUpgradeFail(int i) {
                        callback.onUpgradeFail(i);
                    }
                });
            } else {
                RecoverySystem.installPackage(OtaAgentPolicy.sCx, new File(file));
            }
        } catch (Exception e) {
            e.printStackTrace();
            Trace.e(TAG, "writePackage() Exception: ", e);
            callback.onUpgradeFail(Error.UPGRADE_IOEXCEPTION);
        }
    }

    @Override
    public String getVersion() {
        return CustomConfig.getInstance().version;
    }
}
